
const { exec } = require('../db/index')

class WebChat {

  // 注册
  async register({username, password, avatar}) {
    const data = await exec(`insert into users (username, password, avatar) values (?,?,?)`, [username, password, avatar])
    return { data }
  }

  // async findBroker({ brokerId }) {
  //   const data = await exec(`select * from  users where id=?`, [brokerId])
  //   return { data }
  // }

  // 获取用户信息 , 每次进入页面，就请求这个接口
  async getRecords({userId, brokerId}) {
  // 一下js获取用户信息，任意选择一种：

  // 一、查询多条数据，然后进行判断匹配
  // const records = await exec(`select * from records`)
    // const userData = await exec(`select * from users`)
    // records.forEach(item => {
    //   userData.forEach(val => {
    //     if (item.submitId === val.id) {
    //       item.submit = val.username;
    //       item.avatar = val.avatar
    //     }
    //   })
    // })

    // 二、通过sql多表联查，直接能拿到结果
    const data = await exec(`
      select
      records.id,
      users.username as submit,
      users.avatar,
      records.content,
      records.brokerId,
      records.userId,
      records.submitId
      from records left join users on records.submitId = users.id
    `,)

    return { data  }
  }

}

module.exports = new WebChat;
